class Solution {
public:
    int numDecodings(string s) {
        /*
        int len=s.size();
        if(s[0]=='0'){
            return 0;
        }
        vector<int> res(len+2,1);
        for(int i=1;i<len;i++){
            if(s[i]=='0'){
                if(s[i-1]=='1'||s[i-1]=='2'){
                    res[i+1]=res[i-1];
                } else{
                    return 0;
                }
            } else if((s[i-1]=='1')||(s[i-1]=='2'&&'1'<=s[i]&&s[i]<='6')){
                res[i+1]=res[i]+res[i-1];
            } else{
                res[i+1]=res[i];
            }
        }
        return res[len];
        */
        int len=s.size();
        if(s[0]=='0'){
            return 0;
        }
        int j=1;
        int j1=1;
        int j2=1;
        for(int i=1;i<len;i++){
            if(s[i]=='0'){
                if(s[i-1]=='1'||s[i-1]=='2'){
                    j=j2;
                } else{
                    return 0;
                }            
            } else if((s[i-1]=='1')||(s[i-1]=='2'&&'1'<=s[i]&&s[i]<='6')){
                j=j1+j2;
            } else{
                //i=i1;
            }
            //cout<<"j:"<<j<<",j1:"<<j1<<",j2:"<<j2<<endl;
            j2=j1;
            j1=j;
        }
        return j; 

        /*
        if(s[0]=='0'){
            return 0;
        }
        int cur=1;//当前字符i在内的前面所有字符译码方式数dp[i]
        int prev_2=1;//dp[i-2]
        int prev_1=1;//dp[i-1]
        for(int i=1;i<s.size();i++){
            if(s[i]=='0'){
                if(s[i-1]=='1'||s[i-1]=='2'){
                    cur=prev_2;
                }else{
                    return 0;
                }
            }else{
                if((s[i-1]=='1')||(s[i-1]=='2'&&'1'<=s[i]&&s[i]<='6')){
                    cur=prev_2+prev_1;
                }
            }
            prev_2=prev_1;
            prev_1=cur;
        }
        return cur;
        */
    }
};
